function [flag_equal,diffpos]=isequal_mat(x,y,tol)
% =========================================================================
% function [flag_equal,diffpos]=isequal_mat(x,y,tol)
%
% This is a generalization of the ISEQUAL function 
% X1 is a vector or matrix 
% X2 is a matrix. If X1 is a matrix then they must be of the same size 
% TOL is a tolerance criteria for equality 
% If unspecified will use 1e-16 
% 
% FLAG_EQUAL == 1 if abs( x-y ) < tol for all elements 
% DIFFPOS    location of the differences [], if none 
% Alejandro Justiniano March 29 2010 
% =========================================================================
if nargin < 3 
    tol=1e-16; 
end 
sizex=size(x); 
sizey=size(y); 
if sizex(1)~=sizey(1) 
    error('X and Y must have same number of rows') 
end 
if sizex(2)==1 
    x=repmat( x,[1 sizey(2)] ); 
else 
    if sizex(2)~=sizey(2)
        error('X and Y must have same number of columns when X is not a vector')
    end
end 
flag_equal=max(max(abs(x-y))) <= tol; 
if nargout > 1
    switch flag_equal
        case 1
            diffpos=[];
        case 0
            diffpos=find( abs( x-y ) > tol );
    end
end